package com.hk.yajin.function.members.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseFragment;
import com.hk.yajin.function.members.activity.AboutUsActivity;
import com.hk.yajin.function.members.activity.InviteFriendActivity;
import com.hk.yajin.function.members.activity.LoginAndRegisterActivity;
import com.hk.yajin.function.members.activity.NoticeActivity;
import com.hk.yajin.function.members.activity.RechargeGlodActivity;
import com.hk.yajin.function.members.activity.setting.SelectPictureActivity;
import com.hk.yajin.function.members.activity.setting.SettingActivity;
import com.hk.yajin.function.members.activity.UserAdviceActivity;
import com.hk.yajin.function.messages.BuyMessageActivity;
import com.hk.yajin.function.videos.activity.MyVideoActivity;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.UserGlod;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.io.File;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.app.Activity.RESULT_FIRST_USER;
import static android.app.Activity.RESULT_OK;

/**
 * Created by 伦哥哥 on 2017/6/27.
 */

public class MenuMemberFragment extends BaseFragment {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.user_photo)
    ImageView userPhoto;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.lly_head)
    LinearLayout llyHead;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_buy_message)
    TextView tvVip;
    @BindView(R.id.tv_my_video)
    TextView tvBuy;
    @BindView(R.id.tv_invite)
    TextView tvInvite;
    @BindView(R.id.tv_my_vip)
    TextView tvMyVip;
    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.tv_setting)
    TextView tvSetting;
    @BindView(R.id.tv_about)
    TextView tvAbout;
    @BindView(R.id.tv_advice)
    TextView tvAdvice;
    @BindView(R.id.tv_gold)
    TextView tvGold;

    private Toolbar toolbar;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        mContentView = inflater.inflate(R.layout.fragment_menu_memeber, container, false);
        ButterKnife.bind(this, mContentView);
        initToolBar();
        return mContentView;
    }

    private void initToolBar() {
        toolbar = (Toolbar) mContentView.findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(UiUtil.getColor(R.color.white));
        toolbar.setTitle("");
        toolbar.setNavigationIcon(null);
        TextView textView = (TextView) mContentView.findViewById(R.id.tv_title);
        textView.setText(UiUtil.getString(R.string.title_member));
        textView.setTextColor(UiUtil.getColor(R.color.black_282828));
    }


    @OnClick({R.id.tv_gold,R.id.user_photo, R.id.tv_nickname, R.id.tv_level, R.id.lly_head, R.id.tv_order, R.id.tv_buy_message, R.id.tv_my_video, R.id.tv_invite, R.id.tv_my_vip, R.id.tv_notice, R.id.tv_setting, R.id.tv_about, R.id.tv_advice})
    public void onViewClicked(View view) {
        if (!UserInfoManager.getInstance().isLogin()) {
            startActivity(new Intent(getActivity(), LoginAndRegisterActivity.class));
        } else {
            switch (view.getId()) {

                case R.id.tv_gold:
                    startActivity(RechargeGlodActivity.class);
                    break;
                case R.id.user_photo:
                case R.id.tv_nickname:
                case R.id.tv_level:
                    if (isNetWorkConnect()) {
                        startActivityForResult(new Intent(getActivity(), SelectPictureActivity.class), Activity.RESULT_FIRST_USER);
                    }
                    break;

                case R.id.lly_head:
                    break;
                case R.id.tv_order:
                    //TODO
                    break;
                case R.id.tv_buy_message:
                    startActivity(BuyMessageActivity.class);
                    break;
                case R.id.tv_my_video:
                    startActivity(MyVideoActivity.class);
                    break;
                case R.id.tv_invite:
                    startActivity(new Intent(getActivity(), InviteFriendActivity.class));
                    break;
                case R.id.tv_my_vip:
                    break;
                case R.id.tv_notice:
                    startActivity(new Intent(getActivity(), NoticeActivity.class));
                    break;
                case R.id.tv_setting:
                    startActivity(new Intent(getActivity(), SettingActivity.class));
                    break;
                case R.id.tv_about:
                    startActivity(new Intent(getActivity(), AboutUsActivity.class));
                    break;
                case R.id.tv_advice:
                    startActivity(new Intent(getActivity(), UserAdviceActivity.class));
                    break;
            }
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        if (UserInfoManager.getInstance().isLogin()) {
            setUserData();
            getUserGlod();
        } else {
            UiUtil.showCircleImage(userPhoto, R.drawable.loading_face);
            tvNickname.setText("未登录");
            tvLevel.setText("");
            tvGold.setText("");
        }
    }

    private void setUserData() {
        String headUrl = UserInfoManager.getInstance().getUserInfo().getAvatar();
        if (!TextUtils.isEmpty(headUrl)) {
            UiUtil.showCircleImage(userPhoto, headUrl);
        }
        tvNickname.setText(UserInfoManager.getInstance().getUserInfo().getUser_name());
        tvLevel.setText(UserInfoManager.getInstance().getUserInfo().getVip_yes() == 0 ? "非vip" : "vip");
    }

    private void getUserGlod() {
        showLoadingView();
        Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
        HttpRequestUtils.post(false, UrlConstants.FUNCTION_GET_USER_GOLD, reqParam, false, new HttpCallBack() {

            @Override
            public void onSucced(String data) {
                hideLoadingView();
                UserGlod userGlod = FastjsonUtils.getBeanObject(data, UserGlod.class);
                UserInfoManager.getInstance().getUserInfo().setUserGlod(userGlod);
                tvGold.setText(userGlod.getUsable() + "");
            }

            @Override
            public void onErrorCode(NewWorkBean bean) {
                hideLoadingView();
            }

            @Override
            public void onBadHttp(String tip) {
                hideLoadingView();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RESULT_FIRST_USER && resultCode == RESULT_OK && data != null) {
            String path = data.getStringExtra(SelectPictureActivity.COMPRESS_IMAGE_PATH);
            uploadHeadImage(path);
        }
    }

    private void uploadHeadImage(String path) {
        showLoadingView();
        Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
        reqParam.put("avatar", new File(path));
        HttpRequestUtils.post(false, UrlConstants.FUNCTION_HEAD_IMAGE, reqParam, false, new HttpCallBack() {
            @Override
            public void onSucced(String data) {
                hideLoadingView();
                String headUrl = FastjsonUtils.getJsonString(data, "avatar");
                if (!TextUtils.isEmpty(headUrl)) {
                    UserInfoManager.getInstance().getUserInfo().setAvatar(headUrl);
                    UiUtil.showCircleImage(userPhoto, headUrl);
                }
            }

            @Override
            public void onErrorCode(NewWorkBean bean) {
                hideLoadingView();
                showShortToast(bean.getMsg());
            }

            @Override
            public void onBadHttp(String tip) {
                hideLoadingView();
                showShortToast(tip);
            }
        });
    }
}
